package com.example.phonecall;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = findViewById(R.id.button);
        button.setOnClickListener(view -> {
            if(ActivityCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
                Toast.makeText(MainActivity.this, "電話できない", Toast.LENGTH_SHORT).show();
                return;
            }
            String phoneNumber = "+8613307738246";
            String encodedPhoneNum = null;

            try{
                encodedPhoneNum = URLEncoder.encode(phoneNumber, "UTF-8");
            } catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + encodedPhoneNum)));
        });
    }
}